package basic.study.wantOffer.chapter2;

/**
 * @author Kevin
 * @date 2020/5/3 0:50
 * 二进制中1的个数
 */
public class Problem15 {
    public int NumberOf1_Solution1(int n) {
        int count = 0;
        int flag = 1;
        while (flag != 0) {
            if ((flag & n) != 0) {
                count++;
            }
            flag = flag << 1;
        }
        return count;
    }

    public int NumberOf1_Solution2(int n) {
        int count = 0;
        while (n != 0) {
            count++;
            n = (n-1) & n;
        }
        return count;
    }
}
